package rz.readRemoteImg;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class ReadRemoteImg {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new ReadRemoteImg().getImage();
	}

	public byte[] getImage() {
		
		InputStream is = null;
		//FileOutputStream fos = null;
		try {
			String imgUrl = "http://qlogo2.store.qq.com/qzone/349920141/349920141/50";
			String imgName = "";
			try {
				imgName = imgUrl.substring(imgUrl.lastIndexOf("/")+1);
			} catch (RuntimeException e) {
				e.printStackTrace();
			}
			String suffix = "";
			try {
				suffix = imgUrl.substring(imgUrl.lastIndexOf(".")+1);
			} catch (RuntimeException e) {
				e.printStackTrace();
			}
			URL url = new URL(imgUrl);
			URLConnection connection = url.openConnection();
			url.openConnection();
			is = connection.getInputStream();
			
			//fos = new FileOutputStream("d:/test.jpg");
			int countLength = 0;
			int length = 0;
			
			byte[] b = new byte[10*1024*1024];//10M
			while (true) {
				length = is.read(b, countLength, 1024);
				if (length < 0) {
					break;
				} else {
					countLength += length;
				}
			}
			
			//fos.write(b, 0, countLength);
			return b;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			if (is!=null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			/*if (fos!=null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}*/
		}
	}
}
